<?php

namespace App\Http\Middleware;

use App\Models\User;
use Closure;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Redis;

class ApiMiddleware{

	public function __construct(){

	}


	public function handle($request, Closure $next){
		//跨域
		header("Access-Control-Allow-Origin:*");
		header("Access-Control-Allow-Methods:GET,POST");
		header('Access-Control-Allow-Headers:x-requested-with,content-type');

		//接口认证
		$access_token = $request->input('access_token');
		$timestamp = $request->input('timestamp');
		$data = $request->request->get('data', '');		//$request->request->get()，只取post值
		$str = $data.$timestamp."shop_beitiane";	//拼接字符串

		if(sha1($str) !== $access_token){	//判断接口是否正确
			echo_error(1001,"access_token错误！");
		}

		$data = json_decode(base64_decode($request->request->get('data', '')),true);
		if(isset($data['user_id'])){	//判断参数中是否有用户id
			$user_id = $data['user_id'];
			$user = User::getFind($user_id);
			if(empty($user)){			//判断用户是否存在
				echo_error(2001,"该用户不存在！");
			}
		}




		return $next($request);
	}
}